HttpUtil : Http utility tools
This module is an http util module. It provides some common http tools.
User can use the following code to import the httpUtil
module.
var httpUtil = require('http_util');
Support
The following shows httpUtil
module APIs available for each permissions.
User Mode | Privilege Mode | |
---|---|---|
httpUtil.normalPath | ● | ● |
httpUtil.parseUrl | ● | ● |
httpUtil.IP | ● | ● |
httpUtil.parseHost | ● | ● |
httpUtil.getHostAddr | ● | ● |
httpUtil.getHostAddrAsync | ● | ● |
httpUtil.createError | ● | ● |
HttpUtil Module
httpUtil.normalPath(path)
path
{String} String of http path.- Returns: {String} Http normal path.
Format http path. The http path begin with /
and omit the end /
.
Example
httpUtil.normalPath('/foo/bar'); // Result: /foo/bar
httpUtil.normalPath('/foo/bar/'); // Result: /foo/bar
httpUtil.normalPath('foo/bar'); // Result: /foo/bar
httpUtil.parseUrl(url)
url
{String} Http url.- Returns: {Object} Parsed url object, the attributes as:
protocol
{String} Http protocol. Supporthttp
,https
,ws
,wss
.host
{String} Http host, not include port.port
{String} Http port, default: 80(for http, ws), 443(for https, wss).path
{String} Http url path, include query string.
Parse http url.
Example
httpUtil.parseUrl('http://192.168.7.100:8080/path?name=sss');
/*
* Result:
* { protocol: 'http',
* host: '192.168.7.100',
* port: 8080,
* path: '/path?name=sss'
* }
*/
httpUtil.IP
httpUtil.IP
is a object which enummerate ip type:
IPV4
{Integer} ipv4 type (1).IPV6
{Integer} ipv6 type (2).NONE
{Integer} uncertainty (0);
httpUtil.IP
list ip type. It is usually the result of httpUtil.parseHost
.
httpUtil.parseHost(host)
host
{String} Http ip, not include port.- Returns: {Integer} Ip type that
httpUtil.IP
enumerated .
Parse ip type.
Example
httpUtil.parseHost('192.168.7.100'); // Result: 1
httpUtil.parseHost('1080::8:800:200C:417A'); // Result: 2
httpUtil.getHostAddr(host, port[, domain])
host
{String} Http host. It can be domain name or ip.port
{Integer} Http port.domain
{Integer} Protocol domain. Thedomain
can besocket.AF_INET
orsocket.AF_INET6
, default: socket.AF_INET- Returns: {Null | Object} Socket address. If fail, return
null
value.
Socket address includes following items:
domain
{Integer} Address domain:socket.AF_INET
orsocket.AF_INET6
.addr
{String} Address.port
{Integer} Port.
The httpUtil.getHostAddr()
function get socket address object. If host
provides the form of a domain name, first resolve the ip through dns
module.
Example
httpUtil.getHostAddr('192.168.7.100', 80);
/*
* Result: {
* domain: socket.AF_INET,
* addr: '192.168.7.100',
* port: 80,
* }
*/
httpUtil.getHostAddrAsync(host, port[, domain], callback[, timeout])
host
{String} Http host. It can be domain name or ip.port
{Integer} Http port.domain
{Integer} Protocol domain. Thedomain
can besocket.AF_INET
orsocket.AF_INET6
, default: socket.AF_INETcallback
{Function} Query callback.error
{Error} Query error.saddr
{Object} IP address object.
timeout
{Integer} Query timeout, the default value depends on EdgerOS related configuration.
The httpUtil.getHostAddrAsync()
function get socket address object. If host
provides the form of a domain name, first resolve the ip through dns
module.
Example
getHostAddrAsync('www.sylixos.com', 80, (err, saddr) => {
console.log(saddr.addr);
});
httpUtil.createError([status[, reason]])
status
{Integer} Http status code.reason
{String} The error info.- Returns: {Error} Error object.
The httpUtil.createError()
function is used in the WebApp
program. When the user program encounters an error, it can create an Error
object through this function, and then call next (error)
to return. For more information, please refer to WebApp.
Example
app.get('/', function (req, res, next) {
setTimeout(function () {
try {
throw new (httpUtil.createError(404, 'Not find.'));
} catch (err) {
next(err);
}
}, 1000);
});